package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workcell.workdata.xthings.dto.ManufacturerDto;
import com.workcell.workdata.xthings.dto.ManufacturerVo;
import com.workcell.workdata.xthings.dto.PageVo;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.mapper.ManufacturerMapper;
import com.workcell.workdata.xthings.service.ManufacturerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Admin
 * @description 针对表【t_manufacturer(厂商表)】的数据库操作Service实现
 * @createDate 2023-05-15 14:10:25
 */
@Service
@RequiredArgsConstructor
public class ManufacturerServiceImpl extends ServiceImpl<ManufacturerMapper, Manufacturer>
        implements ManufacturerService {
    private final ManufacturerMapper manufacturerMapper;

    @Override
    public List<ManufacturerDto> manufacturerList(ManufacturerVo manufacturerVo) {
        return manufacturerMapper.manufacturerList(manufacturerVo);
    }

    @Override
    public PageInfo<ManufacturerDto> manufacturerPage(PageVo pageVo, ManufacturerVo manufacturerVo) {
        Page<Object> page = PageHelper.startPage(pageVo);
        List<ManufacturerDto> list = this.manufacturerList(manufacturerVo);
        PageInfo<ManufacturerDto> pageInfo = new PageInfo<>(list);
        page.close();
        return pageInfo;
    }

    @Override
    public Manufacturer selectByManufacturerCode(String manufacturerCode) {
        LambdaQueryWrapper<Manufacturer> lambdaQueryWrapper = Wrappers.lambdaQuery(Manufacturer.class)
                .eq(Manufacturer::getManufacturerCode, manufacturerCode);
        return this.getOne(lambdaQueryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(ManufacturerVo manufacturerVo) {
        Manufacturer oldManufacturer = this.selectByManufacturerCode(manufacturerVo.getManufacturerCode());
        Assert.isNull(oldManufacturer, "厂商ID：{}已存在", manufacturerVo.getManufacturerCode());
        // 如果厂商简称为空，将厂商全称作为厂商简称
        manufacturerVo.setManufacturerShortName(StrUtil.emptyToDefault(manufacturerVo.getManufacturerShortName(), manufacturerVo.getManufacturerFullName()));
        // 封装参数
        Manufacturer manufacturer = Manufacturer.builder()
                .manufacturerCode(manufacturerVo.getManufacturerCode())
                .manufacturerShortName(manufacturerVo.getManufacturerShortName())
                .manufacturerFullName(manufacturerVo.getManufacturerFullName())
                .build();
        return this.save(manufacturer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delete(String manufacturerCode) {
        Manufacturer manufacturer = this.selectByManufacturerCode(manufacturerCode);
        Assert.notNull(manufacturer, "厂商ID{}不存在", manufacturerCode);

        LambdaUpdateWrapper<Manufacturer> lambdaUpdateWrapper = Wrappers.lambdaUpdate(Manufacturer.class)
                .eq(Manufacturer::getManufacturerCode, manufacturerCode);
        return manufacturerMapper.delete(lambdaUpdateWrapper) > 0;
    }

    @Override
    public List<Manufacturer> selectList(ManufacturerVo manufacturerVo) {
        String manufacturerCode = manufacturerVo.getManufacturerCode();
        String manufacturerShortName = manufacturerVo.getManufacturerShortName();
        String manufacturerFullName = manufacturerVo.getManufacturerFullName();
        LambdaQueryWrapper<Manufacturer> manufacturerLambdaQueryWrapper = Wrappers.lambdaQuery(Manufacturer.class)
                .like(StrUtil.isNotEmpty(manufacturerCode), Manufacturer::getManufacturerCode, manufacturerCode)
                .like(StrUtil.isNotEmpty(manufacturerShortName), Manufacturer::getManufacturerShortName, manufacturerShortName)
                .like(StrUtil.isNotEmpty(manufacturerFullName), Manufacturer::getManufacturerFullName, manufacturerFullName)
                .orderByAsc(Manufacturer::getId);
        return manufacturerMapper.selectList(manufacturerLambdaQueryWrapper);
    }
}




